import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:cached_network_image/cached_network_image.dart';

import 'package:flutter_mode/wh/entity/location_movies.dart';
import 'package:flutter_mode/wh/movie_details.dart';

class MyAppWH extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HomePageTab();
  }
}

class _HomePageTab extends State<HomePage> {
  var _isChecked = true;
  LocationMovies? location;

  @override
  void initState() {
    super.initState();
    _getMovieData();
  }

  Future<void> _getMovieData() async {
    final response = await Dio().get(
        "https://api-m.mtime.cn/Showtime/LocationMovies.api?locationId=880");
    setState(() {
      location = LocationMovies.fromJson(response.data);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("王大锤"),
      ),
      drawer: Drawer(
        child: ListView(
          padding: EdgeInsets.zero,
          children: <Widget>[
            UserAccountsDrawerHeader(
                decoration: BoxDecoration(color: Colors.lightBlue),
                currentAccountPicture: CircleAvatar(
                    backgroundImage: NetworkImage(
                        "https://avatars0.githubusercontent.com/u/11703619?s=460&v=4")),
                accountName: Text("wanghai"),
                accountEmail: Text("xiaoanzi@mail.com")),
            ListTile(
              title: Text("设置"),
              leading: Icon(Icons.account_box),
              subtitle: Text("账号相关"),
              onTap: () {},
            ),
            CheckboxListTile(
              controlAffinity: ListTileControlAffinity.platform,
              value: _isChecked,
              title: Text("夜间"),
              onChanged: (bool) {
                // setState会重新执行build方法
                setState(() {
                  _isChecked = bool!;
                });
              },
            )
          ],
        ),
      ),
      body: RefreshIndicator(
          child: _getContentBody(context), onRefresh: _getMovieData),
    );
  }

  _getContentBody(BuildContext context) {
    if (location?.ms != null) {
      return ListView.separated(
          physics: const AlwaysScrollableScrollPhysics(),
          itemCount: location!.ms!.length,
          separatorBuilder: (content, index) => Divider(height: 0),
          itemBuilder: (context, i) =>
              _getListViewItem(context, location!.ms![i]!));
    } else {
      return Center(
        child: CircularProgressIndicator(),
      );
    }
  }

  _getListViewItem(BuildContext context, HotMovie hotMovie) {
    const _textStyle = TextStyle(color: Colors.grey, fontSize: 12);
    return InkWell(
        onTap: () => Navigator.push(context, MaterialPageRoute(builder: (context){
          return MovieDetails(hotMovie.img);
        })),
        child: Padding(
            padding: EdgeInsets.all(10),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: <Widget>[
                Hero(
                    tag: hotMovie.img!,
                    transitionOnUserGestures:true,
                    child: CachedNetworkImage(
                      imageUrl: hotMovie.img!,
                      width: 80,
                      height: 120,
                    )),
                Expanded(
                  child: Container(
                      height: 120,
                      child: Padding(
                        padding: EdgeInsets.only(left: 10),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Text(
                              hotMovie.t!,
                              style: TextStyle(fontSize: 16),
                            ),
                            Text("简介：${hotMovie.commonSpecial}",
                                style: _textStyle),
                            Text("导演：${hotMovie.dN}", style: _textStyle),
                            Text("演员：${hotMovie.actors}", style: _textStyle)
                          ],
                        ),
                      )),
                )
              ],
            )));
  }
}
